$(function(){

	prHeight();
    $(window).resize(function () {
        prHeight();
    });

	//左侧导航的切换
	$(document).on("click",".left-nav>li>a",function(){
        var $this = $(this),
            $parent = $this.closest("ul");
		$parent.children("li").removeClass("active");
		$parent.find("li ul").slideUp();
		$this.parent("li").addClass("active");
		$this.next("ul").slideDown();
	})
	.on("click",".left-subnav>li",function(){
		$(this).closest("ul").find("li").removeClass("active");
		$(this).addClass("active");
	})

	//表格标签的切换
	$(document).on("click",".tags-nav ul li",function(){
		$(this).closest("ul").find("li").removeClass("active");
		$(this).addClass("active");
	})

    //隐藏弹窗  
    $(document).on("click",".closewindow",function(){
        closeWindow(this);
        $(".tip-bottom input:last").click();
    })

    //输入框移除焦点时验证表单
    $(".report-form").on("blur","input[type='text'],input[type='password'],textarea",function(){
        var $this = $(this),
            $val = $this.val(),
            isMust = $this.next("i").length;

        removeWTip(this);
        if (isMust && $val.replace(/\s/g,"")==="") {
            addWTip(this,"不能为空！");
        }
    })

    //输入框输入时验证表单
    $("input[type='text'],input[type='password'],textarea").bind('input propertychange', function() {
        var $this = $(this),
            $val = $this.val(),
            isMust = $this.next("i").length,
            $btn = $this.closest(".report-form-body").find(".form-bigbtns").children(".green-btn");

        if (isMust && $val.replace(/\s/g,"") != "") {
            removeWTip(this);
        } 
        if ($(".error-tip").length == 0) {
            $btn.prop("disabled",false);  
        }
    })

})

//高度自适应
jQuery.fn.extend({
    autoHeight: function () {
        return this.each(function () {
            var $this = jQuery(this);
            if (!$this.attr('_initAdjustHeight')) {
                $this.attr('_initAdjustHeight', $this.outerHeight());
            }
            _adjustH(this).on('input', function () {
                _adjustH(this);
            });
        });
        function _adjustH(elem) {
            var $obj = jQuery(elem);
            return $obj.css({height: $obj.attr('_initAdjustHeight'), 'overflow-y': 'hidden'})
                .height(elem.scrollHeight);
        }
    }
});

// textarea使用高度自适应
$(function () {
    $('.template-table textarea').autoHeight();
});


/*右侧最低高度*/
function prHeight(){
	var rheight = $(window).height() - 80;
	$(".pbody-right").css("height",rheight+"px");
}


// 关闭弹窗
function closeWindow(){
    $window = $(".showwindow");
    $window.animate({opacity: '0'},"fast",function(){
        $window.hide();
        $window.css("opacity","1");
        $('[formTo="tip_window"]').remove();
        resetForm();
    }); 
}

// 关闭提示弹窗
function closeTipWindow(){
    $window = $(".showwindow[formto='tip_window']");
    $window.animate({opacity: '0'},"fast",function(){
        $window.hide();
        $('[formTo="tip_window"]').remove();
    });
}


/*提示弹窗*/
/*tipStyle  弹窗类型  ask、success、wrong、warming*/
/*tipP 弹窗主内容*/
/*tipSpan 弹窗次内容*/
/*btn1 按钮1*/
/*color1 按钮1颜色*/
/*fun1 按钮1方法*/
/*btn2 按钮2*/
/*color2 按钮2颜色*/
/*fun2 按钮2方法*/
function addTipHtml(tipStyle,tipP,tipSpan,btn1,color1,fun1,btn2,color2,fun2){
    var btn1Html="",
        btn2Html="";
    if(btn1){
        btn1Html = '<input type="button" value="'+btn1+'" class="btn '+color1+'-btn" />';
    }
    if (btn2) {
        btn2Html = '<input type="button" value="'+btn2+'" class="btn '+color2+'-btn" />';
    }
    $(".wrapper").append('<div class="showwindow" formTo="tip_window" style="display:block;">'+
                            '<div class="bigshadow closewindow"></div>'+
                            '<div class="window-tip '+tipStyle+'-tip">'+
                                '<div class="tip-content">'+
                                    '<p>'+tipP+
                                        '<span>'+tipSpan+'</span>'+
                                    '</p>'+
                                '</div>'+
                                '<div class="tip-bottom">'+
                                    btn1Html+
                                    btn2Html+
                                '</div>'+
                            '</div>'+
                        '</div>');

    $('[formTo="tip_window"]').find('[value="'+btn1+'"]').click(fun1);
    $('[formTo="tip_window"]').find('[value="'+btn2+'"]').click(fun2);
}


/*检测表单必填项是否为空*/
function checkWTip(e){

    var $parent = $(e).closest(".report-form-body");
    if ($parent.length > 0) {
        var mustI = $parent.find("i"),
            j="0";

        for (var i = 0; i < mustI.length; i++) {
            var $input = $(mustI[i]).prev();
                $val = $input.val();

            if ($val.replace(/\s/g,"")==="") {
                removeWTip($input);
                addWTip($input,"不能为空！");
                $(e).prop("disabled",true);
                j++;
            }
        }
        j = parseFloat(j) + parseFloat($parent.find(".error-tip").length);
        if (j!="0") return false;
        else return true;
    }
    else return true;
}

function addWTip(e,tipText){
    var $this = $(e),
        $parent = $this.closest(".input-row"),
        $Attr = $parent.children("span").text();
    var wrong_tip1 = '<em class="error-tip">',
        wrong_tip2 = '</em>';

    $this.addClass("error");
    $this.closest("div").append(wrong_tip1 + $Attr + tipText + wrong_tip2);
}

function removeWTip(e){
    $(e).removeClass("error");
    $(e).closest(".input-row").find(".error-tip").remove();
}


// 重置表单信息
function resetForm() {
    var $form = $(".report-form-body");
    $form.find("input[type='text']").val("");
    $form.find("input[type='password']").val("");
    $form.find("select").prop("disabled",false);
    $form.find("select option").prop("selected", false);
    $form.find("input[type='radio']").prop("checked", false);
    $form.find("input[type='checkbox']").prop("checked", false);
    $form.find("textarea").val("");
    $(".ipt.error").removeClass("error");
    $(".error-tip").remove();
    $(".form-bigbtns .bigbtn").prop("disabled", false);
    
    //重置绩效模版弹窗子表格
    if ($(".window-form").hasClass("assess-form")) {
        $(".quarter-sub-table").find("tbody tr:not(.middle-total)").remove();
        $(".year-sub-table").find("tbody tr").remove();
    }
}


//数字转汉字
function NoToChinese(num) { 
    if (!/^\d*(\.\d*)?$/.test(num)) { 
        return false; 
    } 
    var AA = new Array("零", "一", "二", "三", "四", "五", "六", "七", "八", "九"); 
    var BB = new Array("", "十", "百", "千", "万", "亿", "点", ""); 
    var a = ("" + num).replace(/(^0*)/g, "").split("."), 
        k = 0, 
        re = ""; 

    for (var i = a[0].length - 1; i >= 0; i--) { 
        if (k % 4 == 2 && a[0].charAt(i + 2) != 0 && a[0].charAt(i + 1) == 0) {
            re = AA[0] + re
        }; 
        if (a[0].charAt(i) != 0){
            re = AA[a[0].charAt(i)] + BB[k % 4] + re; 
            k++;
        }
    } 
    return re; 
} 